package api

import (
	"go-proxy/domain/message"
	"go-proxy/initialize"
	"go-proxy/service"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type SystemController struct {
	log    *zap.Logger
	system service.ISystemService
}

func RegisterSystemController(group *gin.RouterGroup) {
	k := &SystemController{
		log:    initialize.Logger,
		system: service.System,
	}

	// 获取节点列表
	system := group.Group("system")
	system.POST("login", k.Login)
}

// Login 登录
func (k *SystemController) Login(c *gin.Context) {
	var (
		param  message.UserLoginRequest
		result *message.UserLoginResponse
		err    error
	)

	if err = c.ShouldBindBodyWithJSON(&param); err != nil {
		c.JSON(200, message.Fail[string](message.RequestParamError, err.Error()))
		return
	}

	if result, err = k.system.Login(&param); err != nil {
		c.JSON(200, message.Fail[string](message.LoginFailed, err.Error()))
		return
	}

	c.JSON(200, message.Message[*message.UserLoginResponse](message.LoginSuccess, result))
}
